<?php
class Library_Model_DangKyTheMapper extends Library_Model_DataMapperAbstract{
	protected static $_instance = null;
	private $_paginator = null;
	
	public function dangkyList()
	{
		$db = $this->getDb();
		$selectDkkh = $db->select()
						->from('tbl_dangkythe');
		$dkthes = $db->fetchAll($selectDkkh);
				
		$dktheArray = array();
		foreach($dkthes as $dkthe)
		{	
			$chinhanhMapper = Library_Model_ChiNhanhMapper::getInstance();
			$chinhanh = $chinhanhMapper->searchChiNhanhById($dkthe['MaChiNhanh']);
			$dkthe['tbl_chinhanh'] = $chinhanh;
			
			$dktheArray[] = $this->_populate($dkthe);
		}
		return $dktheArray;
	}
	
	// hien thi
	public function _populate($data)
	{
		$obj = new Library_Model_DangKyThe();
		$obj->setId($data['MaDangKy']);
		$obj->setHovaten($data['HoVaTen']);
		$obj->setNgaysinh($data['NgaySinh']);
		$obj->setDiachithuongtru($data['DiaChiThuongTru']);
		$obj->setDiachilienlac($data['DiaChiLienLac']);
		$obj->setDienthoai($data['DienThoai']);
		$obj->setCmnd($data['CMND']);
		$obj->setNgaycap($data['NgayCap']);
		$obj->setNoicap($data['NoiCap']);
		$obj->setTencoquan($data['TenCoQuan']);
		$obj->setDiachicoquan($data['DiaChiCoQuan']);
		$obj->setDienthoaicoquan($data['DienThoaiCoQuan']);
		$obj->setMachinhanh($data['MaChiNhanh']);
		$obj->setChinhanh($data['tbl_chinhanh']);
		$obj->setNgaydangky($data['NgayDangKy']);
		return $obj;
	}
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();		
		$data = array(
				'HoVaTen'				=>$obj->getHovaten(),
				'NgaySinh'				=>$obj->getNgaysinh(),
				'DiaChiThuongTru'	=>$obj->getDiachithuongtru(),
				'DiaChiLienLac'		=>$obj->getDiachilienlac(),
				'DienThoai'				=>$obj->getDienthoai(),				
				'CMND'					=>$obj->getCmnd(),
				'NgayCap'				=>$obj->getNgaycap(),
				'NoiCap'					=>$obj->getNoicap(),
				'TenCoQuan'			=>$obj->getTencoquan(),
				'DiaChiCoQuan'		=>$obj->getDiachicoquan(),
				'DienThoaiCoQuan'		=>$obj->getDienthoaicoquan(),
				
				'MaChiNhanh'			=>$obj->getMachinhanh(),
				'NgayDangKy'		=>$obj->getNgaydangky()
				);
		$db->insert('tbl_dangkythe',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data = array(
				'HoVaTen'				=>$obj->getHovaten(),
				'NgaySinh'				=>$obj->getNgaysinh(),
				'DiaChiThuongTru'	=>$obj->getDiachithuongtru(),
				'DiaChiLienLac'		=>$obj->getDiachilienlac(),
				'DienThoai'				=>$obj->getDienthoai(),				
				'CMND'					=>$obj->getCmnd(),
				'NgayCap'				=>$obj->getNgaycap(),
				'NoiCap'					=>$obj->getNoicap(),
				'TenCoQuan'			=>$obj->getTencoquan(),
				'DiaChiCoQuan'		=>$obj->getDiachicoquan(),
				'DienThoaiCoQuan'	=>$obj->getDienthoaicoquan(),
				
				'MaChiNhanh'			=>$obj->getMachinhanh(),
				'NgayDangKy'		=>$obj->getNgaydangky()
		);
		$where = $db->quoteInto('MaDangKy = ?',$obj->getId());
		$db->update('tbl_dangkythe',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaDangKy = ?',$id);
		$db->delete('tbl_dangkythe',$where);
	}
	
	// Goi chinh no
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();				
		}
		return self::$_instance;
	}
}